<template>
  <div
    class="chart-container"
    v-loading="loading"
    element-loading-text="加载中..."
  >
    <div ref="chartRef" style="width: 100%; height: 385px"></div>
  </div>
</template>

<script>
import * as echarts from "echarts";

export default {
  name: "StatisticsChart",
  props: {
    personalList: {
      type: Array,
      default: () => [],
    },
  },
  data() {
    return {
      loading: true,
      chart: null,
      chartData: {
        dates: [],
        loginCounts: [],
        resumeRefresh: [],
        resumeSubmit: [],
      },
    };
  },
  watch: {
    personalList: {
      handler(newVal) {
        if (newVal && newVal.length > 0) {
          this.loading = true;

          const timestamps = newVal[0] || [];
          const loginData = newVal[1] || [];
          const resumeRefreshData = newVal[2] || [];
          const resumeSubmitData = newVal[3] || [];

          this.chartData = {
            dates: timestamps
              .map((timestamp) => this.$options.filters.formatDate(timestamp))
              .reverse(),
            loginCounts: [...loginData].reverse(),
            resumeRefresh: [...resumeRefreshData].reverse(),
            resumeSubmit: [...resumeSubmitData].reverse(),
          };

          this.$nextTick(() => {
            if (!this.chart) {
              this.chart = echarts.init(this.$refs.chartRef);
            }
            this.setChartOption();
            this.loading = false;
          });
        }
      },
      immediate: true,
    },
  },
  created() {
    // console.log(this.personalList);
  },
  mounted() {
    window.addEventListener("resize", () => {
      this.chart && this.chart.resize();
    });
  },
  beforeDestroy() {
    window.removeEventListener("resize", () => {
      this.chart && this.chart.resize();
    });
    if (this.chart) {
      this.chart.dispose();
      this.chart = null;
    }
  },
  methods: {
    setChartOption() {
      const option = {
        tooltip: {
          trigger: "axis",
          axisPointer: { type: "line" },
          formatter: function (params) {
            let result = params[0].axisValue + "<br/>";
            params.forEach((item) => {
              result += item.seriesName + ": " + item.value + "<br/>";
            });
            return result;
          },
        },
        legend: {
          data: ["登录数", "简历刷新", "简历投递"],
          top: 0,
        },
        grid: {
          left: "3%",
          right: "4%",
          bottom: "3%",
          containLabel: true,
        },
        xAxis: {
          type: "category",
          boundaryGap: false,
          data: this.chartData.dates,
          axisLine: {
            lineStyle: { color: "#999" },
          },
          axisTick: { show: false },
        },
        yAxis: {
          type: "value",
          max: 70,
          splitNumber: 6,
          axisLine: { show: false },
          axisTick: { show: false },
          splitLine: {
            lineStyle: {
              type: "dashed",
              color: "#DDD",
            },
          },
        },
        series: [
          {
            name: "登录数",
            type: "line",
            data: this.chartData.loginCounts,
            smooth: true,
            symbol: "circle",
            symbolSize: 6,
            itemStyle: { color: "#409EFF" },
            lineStyle: { width: 2 },
          },
          {
            name: "简历刷新",
            type: "line",
            data: this.chartData.resumeRefresh,
            smooth: true,
            symbol: "circle",
            symbolSize: 6,
            itemStyle: { color: "#FF4D4F" },
            lineStyle: { width: 2 },
          },
          {
            name: "简历投递",
            type: "line",
            data: this.chartData.resumeSubmit,
            smooth: true,
            symbol: "circle",
            symbolSize: 6,
            itemStyle: { color: "#52C41A" },
            lineStyle: { width: 2 },
          },
        ],
      };
      this.chart && this.chart.setOption(option);
    },
  },
};
</script>

<style scoped>
.chart-container {
  padding: 20px;
  background: #fff;
  border-radius: 4px;
  min-height: 385px;
}
</style>
